<?php
require_once('MySqlConnection.php');
require_once('Country.php');

class CountryDAL{

	private $_connection;
	
	//SINGLETON PATTERN
    private static $_instance = null;
	public static function Instance(){
		if (self::$_instance == null) self::$_instance = new CountryDAL();
		return self::$_instance;
	}
	//SINGLETON PATTERN END
	
	private function __construct(){
		include_once('./include/db.inc');
		$this->_connection = MySqlConnection::Instance($server,$user,$password,$database);
	}
	
	public function getCountries(){
		$this->_connection->Open();
		$sqlString = "select * from country";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'country');
	}
	
	public function getCountryById($countryId){
		$this->_connection->Open();
		$sqlString = "select * from country where id = '{$countryId}'";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'country');
	}
	
	public function addCountry($country){
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("INSERT INTO country(name,mapurl) VALUES(?,?)")) {
			$stmt->bind_param("ss",$country['name'],$country['mapurl']);
			$success = $stmt->execute();
			if(!$success){
				echo "Inserting country failed!";
			}
		}
		else{
			
			echo "Failed preparing insert country statement!";
		}
		$mysqli -> close();
	}
	
	public function removeCountry($countryId){
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("DELETE FROM country WHERE id = ?")) {
			$stmt->bind_param("i", $countryId);
			$success = $stmt->execute();
			if(!$success){
				echo "Removing country failed!";
			}
		}
		else{
			echo "Failed preparing delete country statement!";
		}
		$mysqli -> close();
	}
	
	public function updateCountry($country) {
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("UPDATE country SET name = ? WHERE id = ?")) {
			$stmt->bind_param("si", $country['name'], $country['id']);
			$success = $stmt->execute();
			if(!$success){
				echo "Updating country failed!";
			}
		}
		else{
			echo "Failed preparing update country statement!";
		}
		$mysqli -> close();
	}
	
	//MAPPING	
	private function Mapping($queryData,$resultType){
		foreach($queryData as $data){
			$resultArray[] = $this->Map($data,$resultType);
		}
		return $resultArray;
	}
	
	private function Map($data,$resultType){
		$object = new Country($data->id,$data->name,$data->mapurl);
		return $object;
	}
	//MAPPING END
	
}

